package demo1;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-18
 * Time: 18:03
 */

public class MyQueue {
    Stack<Integer> stack1; //这个负责入队列
    Stack<Integer> stack2; //这个负责出队列

    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        //如果把stack1的元素都搬运到stack2了，但是stack2还为空，则
        if (stack2.isEmpty()) {
            throw new RuntimeException("队列为空，无法pop()！");
        }
        return stack2.pop();
    }

    public int peek() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        //如果把stack1的元素都搬运到stack2了，但是stack2还为空，则
        if (stack2.isEmpty()) {
            throw new RuntimeException("队列为空，无法peek()！");
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty()&&stack2.isEmpty();
    }
}
